
var service = require('./service');
var express = require('express');
var LocalStorage = require('node-localstorage').LocalStorage;
localStorage = new LocalStorage('./scratch');
// 1. 创建一个路由容器
var router = express.Router();


router.get('/index', function (req, res) {
  res.render('index.html')
});
//订单
router.get('/order', function (req, res) {
  service.find(function (err, list) {
    res.render('order.html', {
      list: list
    })
  })
});

router.post('/order', function (req, res) {
  var isLogin= localStorage.getItem("isLogin")||"0";
  if(isLogin==="1") {//已登录
    service.saveOrder(req.body, function (user) {
      res.redirect('/order');
    })
  }else{
    res.redirect('/login');
  }
});

router.get('/login', function (req, res) {
  res.render('login.html')
});

router.post('/login', function (req, res) {
  service.login(req.body, function (user) {
    res.redirect('/index');
  })
});



router.get('/add', function (req, res) {
  res.render('add.html')
});

router.post('/add', function (req, res) {
  service.save(req.body, function (err) {
    res.redirect('/login')
  })
});

router.get('/delete', function (req, res) {
  service.deleteById(req.query.id, function (err) {
    res.redirect('/order')
  })
});





// 3. 把 router 导出
module.exports = router;

